/*
Copyright (c) 2008, Simon Lebettre
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the rpginventory nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.rpginventory.hud;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JToolBar;

import org.rpginventory.game.GameClient;
import org.rpginventory.hud.components.BagPanel;
import org.rpginventory.hud.components.InventoryPanel;
import org.rpginventory.hud.components.QuickSlotBar;
import org.rpginventory.hud.components.SpellsPanel;
import org.rpginventory.model.Bag;
import org.rpginventory.model.SpellBook;

import com.jme.input.MouseInput;
import com.jmex.awt.swingui.JMEDesktopState;
import com.jmex.awt.swingui.dnd.JMEDragAndDrop;

public class HudGameState extends JMEDesktopState {

	private BagPanel bag1;
	private SpellsPanel spellsFrame;
	private JInternalFrame inventoryFrame;
	private JDesktopPane jDesktopPane;

	public HudGameState() {
		// true =  variable height & width ( = size adjusted to display)
		super(true);

	}

	protected void postInit() {

		jDesktopPane = getDesktop().getJDesktop();
		new JMEDragAndDrop(this.getDesktop());
	}

	@Override
	protected void buildUI() {

		// this would better be in the constructor but the constructor calls
		// init which calls buildUI
		// super.init would be good too but super.init is private
		postInit();

		addBag();
		addInventory();
		addSpellBook();
		addQuickSlotBar();
		addCommandsBar();

		// Set the cursor to visible
		MouseInput.get().setCursorVisible(true);
	}

	private void addInventory() {
		
		//this.inventoryFrame = new JInternalFrame("inventory",false,true);
			
		InventoryPanel panel = new InventoryPanel(this,"inventoryx");
		panel.setVisible(true);
	//	this.inventoryFrame.setContentPane(panel);
		//inventoryFrame.setVisible(true);
		jDesktopPane.add(panel);

	}

	private void addSpellBook() {
		SpellBook book = GameClient.myGameState.getPlayer().getPlayerEntity().getSpellBook();
		this.spellsFrame = new SpellsPanel(this, "Spells", book);

		spellsFrame.setVisible(true);

		jDesktopPane.add(this.spellsFrame);

	}

	private void addCommandsBar() {
		JToolBar toolbar = new JToolBar();
		toolbar.setLocation(0, this.jDesktopPane.getHeight() - 50 - 40);// 50 is
		// quickslot
		// height
		toolbar.setSize(32 * 5, 32);
		toolbar.setLayout(new GridLayout());

		toolbar.add(buildCommandButton("bag_btn", "/img/box.png", bag1));
		toolbar.add(buildCommandButton("inv_btn", "/img/charactersheet.png", inventoryFrame));
		toolbar.add(buildCommandButton("spell_btn", "/img/shoot.png", spellsFrame));
		
		JButton quitButton = new JButton("Quit");
		quitButton.setSize(quitButton.getPreferredSize());
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// game.finish();
				GameClient.myGameState.quit();
			}
		});
		toolbar.add(quitButton);

		jDesktopPane.add(toolbar);

	}

	private JButton buildCommandButton(String name, String iconPath, final JComponent component) {
		JButton button = new JButton(name);
		URL imageURL = this.getClass().getResource(iconPath);
		if (imageURL == null) {
			button.setText(iconPath);
		} else {
			ImageIcon icon = new ImageIcon(imageURL);
			icon.setImage(icon.getImage().getScaledInstance(32, 32, 16));
			button.setIcon(icon);
		}
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				component.setVisible(!component.isVisible());
			}
		});
		return button;
	}

	public void addBag() {

		Bag bag = GameClient.myGameState.getPlayer().getPlayerEntity().getBag(1);
		this.bag1 = new BagPanel(this, "Bag", bag);

		bag1.setVisible(true);

		jDesktopPane.add(bag1);

	}

	public void addQuickSlotBar() {

		QuickSlotBar toolbar = new QuickSlotBar(this, "quickslot", 64, 64);

		toolbar.setVisible(true);
		jDesktopPane.add(toolbar);
	}
}
